import * as constants from "./constants";
import { fromJS } from "immutable";
import {
  getAllMetaDataTypes,
  getAllCount,
  deleRecord,
  addNew,
  viewInfo,
  editCate,
  searchIn,
  onlySearchType,
  unitSearch,
  getListInfo,
  metaAdd,
  deleListRecord,
  editList,
  downloadModal,
  exportMo,
} from "../../../../api/metaData/metaData";
import Search from "antd/lib/input/Search";
import { Descriptions } from "antd";
import { actionCreators } from ".";

// const getCategory = (category) => ({
//   type: constants.GET_CATEGORY,
//   category: fromJS(category),
// });

// export const allCategoryOperation = () => {
//   return(dispatch) => {
//     allCategoryAxios().then((res) => {
//       if(res){
//         if(res.data.flag){
//           const category = res.data.data;
//           dispatch(getCategory(category));
//         }
//       }
//     });
//   }
// };
const allList = (result, totalPage, total, totalSize) => ({
  type: constants.GET_ALL_LIST,
  result,
  totalPage,
  total,
  totalSize,
});
const getList = (dispatch) => {
  const pro = new Promise((resolve, reject) => {
    getAllMetaDataTypes().then((res) => {
      if (res.status === 200) {
        const result = res.data.data;
        const totalSize = result.length;
        const totalPage = Math.ceil(result.length / 10);
        const total = Math.ceil(result.length / 10);
        const metaIdsArr = [];
        result.forEach((element) => {
          metaIdsArr.push(element.metaId);
          resolve(metaIdsArr);
        });
        dispatch(allList(result, totalPage, total, totalSize));
      }
    });
  });
  pro.then(function (metaIdsArr) {
    getAllCount(metaIdsArr).then((res) => {
      const result = res.data.data;
      dispatch({
        type: "render_length",
        result,
      });
    });
  });
};
export const getAllList = () => {
  return (dispatch) => {
    getList(dispatch);
  };
};
export const changePageNum = (current, pageSize) => {
  return (dispatch) => {
    dispatch({
      type: constants.CHANGE_PAGE,
      current,
      pageSize,
    });
  };
};
export const deleteRecord = (metaId) => {
  return (dispatch) => {
    deleRecord(metaId).then((res) => {
      if (res.status === 200) {
        getList(dispatch);
      }
    });
  };
};

export const newAdd = (name, description, number) => {
  return (dispatch) => {
    addNew(name, description, number).then((res) => {
      if (res.status === 200) {
        getList(dispatch);
      }
    });
  };
};
export const changePageSize = (pageNum) => {
  return (dispatch) => {
    dispatch({
      type: "change_size",
      pageNum,
    });
  };
};
export const changeAdd = () => {
  return (dispatch) => {
    dispatch({
      type: constants.CHANGE_ADD,
      addVisible: false,
    });
  };
};
export const view = (recordId, recordName) => {
  return (dispatch) => {
    viewInfo(recordId).then((res) => {
      const result = res.data.data;
      const totalSize = result.length;
      const totalPage = Math.ceil(result.length / 10);
      const total = Math.ceil(result.length / 10);
      dispatch({
        type: constants.VIEW,
        recordId,
        recordName,
        result,
        totalSize,
        totalPage,
        total,
      });
    });
  };
};
export const editor = (record) => {
  return (dispatch) => {
    dispatch({
      type: constants.EDITOR,
      record,
    });
  };
};
export const editCategory = (name, description, id) => {
  return (dispatch) => {
    editCate(name, description, id).then((res) => {
      getList(dispatch);
    });
  };
};
const searchItem = (result, totalSize, totalPage, total) => ({
  type: constants.SEARCH_ITEM,
  result,
  totalSize,
  totalPage,
  total,
});
export const searchInfo = (userId, number) => {
  return (dispatch) => {
    searchIn(userId, number)
      .then((res) => {
        if (res.status === 200) {
          const result = res.data.data.list;
          if (result.length === 0) {
            return;
          }
          const totalSize = result.length;
          const totalPage = Math.ceil(result.length / 10);
          const total = Math.ceil(result.length / 10);
          dispatch(searchItem(result, totalSize, totalPage, total));
        }
      })
      .catch((err) => {
        console.log(err);
      });
    return;
  };
};
const searchInfoItem = (result, totalSize, totalPage, total) => ({
  type: constants.SEARCH_INFO_ITEM,
  result,
  totalSize,
  totalPage,
  total,
});
export const searchData = (type, cnname, enname, metaid) => {
  return (dispatch) => {
    switch (type) {
      case "全部":
        type = null;
        break;
      case "国标":
        type = "10";
        break;
      case "私有":
        type = "11";
        break;
      default:
        break;
    }
    if (type && cnname === undefined && enname === undefined) {
      onlySearchType(type, metaid).then((res) => {
        if (res.status === 200) {
          const result = res.data.data.list;
          if (result.length === 0) return;
          const totalSize = result.length;
          const totalPage = Math.ceil(result.length / 10);
          const total = Math.ceil(result.length / 10);
          dispatch(searchInfoItem(result, totalSize, totalPage, total));
        }
      });
    } else if (cnname || enname) {
      unitSearch(type, cnname, enname, metaid).then((res) => {
        if (res.status === 200) {
          const result = res.data.data.list;
          if (result.length === 0) return;
          const totalSize = result.length;
          const totalPage = Math.ceil(result.length / 10);
          const total = Math.ceil(result.length / 10);
          dispatch(searchInfoItem(result, totalSize, totalPage, total));
        }
      });
    }
  };
};
export const getMetaDataInfo = (recordId) => {
  return (dispatch) => {
    getListInfo(recordId).then((res) => {
      const result = res.data.data;
      const totalSize = result.length;
      const totalPage = Math.ceil(result.length / 10);
      const total = Math.ceil(result.length / 10);
      dispatch({
        type: "get_metadatainfo",
        result,
        totalSize,
        totalPage,
        total,
      });
    });
  };
};
export const addMetaDa = (addValues) => {
  return (dispatch) => {
    metaAdd(addValues).then((res) => {});
  };
};
export const deleteListRecord = (dataId) => {
  return (dispatch) => {
    deleListRecord(dataId)
      .then((res) => {})
      .catch((err) => {
        console.log(err);
      });
  };
};
export const editListData = (editValues) => {
  return (dispatch) => {
    editList(editValues)
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });
  };
};
export const changeInfoSize = (pageNum) => {
  return (dispatch) => {
    dispatch({
      type: "change_info_size",
      pageNum,
    });
  };
};
export const changeInfoPageNum = (current, pageSize) => {
  return (dispatch) => {
    dispatch({
      type: constants.CHANGE_INFO_PAGE,
      current,
      pageSize,
    });
  };
};
export const modalData = () => {
  return (dispatch) => {
    downloadModal()
      .then((res) => {
        const elink = document.createElement("a");
        elink.download = "模板.zip";
        const blob = new Blob([res.data]);
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
      })
      .catch((err) => {
        console.log(err);
      });
  };
};
export const exportData = (sendMetaId) => {
  return (dispatch) => {
    exportMo(sendMetaId)
      .then((res) => {
        const elink = document.createElement("a");
        elink.download = "metadata.xls";
        const blob = new Blob([res.data]);
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
      })
      .catch((err) => {
        console.log(err);
      });
  };
};
